﻿using OpenMms.Common.Log;
using OpenMms.Domain.Interface;
using OpenMms.Domain.Interface.User;
using OpenMms.Domain.Models;
using OpenMms.Domain.ReponseModels;
using OpenMms.Domain.RequestModels;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace OpenMms.Domain.BLL.User
{
    /// <summary>
    /// 修改密码
    /// </summary>
    public class ModifyPwdService : IModifyPwdService
    {
        #region 初始化
        /// <summary>
        /// 数据访问
        /// </summary>
        private IBLLConstruct<UserInforMations> _repository;
        public ModifyPwdService(IBLLConstruct<Models.UserInforMations> repository)
        {
            this._repository = repository;
        }
        #endregion

        #region 修改密码操作
        /// <summary>
        /// 修改密码操作
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        public async Task<Response> ModifyPwd(ModifyPwdReq req)
        {
            Response _resp = new Response();
            _resp.Code = 200;
            _resp.Message = "修改密码成功!";
            if(! await CheckPassword(req))
            {
                _resp.Code = 201;
                _resp.Message = "初始密码输入错误!";
                return _resp;
            }
            MyLog.Log(req, "User", "修改密码");
            if (! await ModifyPassword(req))
            {
                _resp.Code = 202;
                _resp.Message = "修改密码失败!";
                return _resp;
            }
            return _resp;
        }
        #endregion

        #region 判断原始密码
        /// <summary>
        /// 判断原始密码是否正确
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        protected async Task<bool> CheckPassword(ModifyPwdReq _model)
        {
            var userinfo = await _repository.FindAsync(u => u.UserCode == _model.UserCode);
            if (userinfo == null)
            {
                return false;
            }
            if (_model.Type == PasswordType.一级密码)
            {
                return userinfo.UserPassWord == _model.OldPassword;
            }
            if (_model.Type == PasswordType.二级密码)
            {
                return userinfo.UserSecondPassWord == _model.OldPassword;
            }
            return false;
        }
        #endregion

        #region 修改密码
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="_model"></param>
        /// <returns></returns>
        protected async Task<bool> ModifyPassword(ModifyPwdReq _model)
        {
            if (_model.Type == PasswordType.一级密码)
            {
             return  await _repository.UpdateAsync(u => u.UserCode == _model.UserCode, u => new Models.UserInforMations() {
                    UserPassWord = _model.NewPassword
                })>0;
                
            }
            if (_model.Type == PasswordType.二级密码)
            {
              return await _repository.UpdateAsync(u => u.UserCode == _model.UserCode, u => new Models.UserInforMations()
                {
                    UserSecondPassWord = _model.NewPassword
                })>0;
            }
            return false;
        }
        #endregion

    }
}
